# coding:utf-8
"""
    @project: automated_testing_platform
    @Author：caicaizi
    @file： base_web_driver.py
    @date：2023/5/17 23:29
"""
import pytest
from time import sleep
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebElement, WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.opera import OperaDriverManager


class BaseWebDriver:
    """定义web驱动类"""
    def __init__(self, executor=None, driver_name='chrome'):
        self.driver_name = driver_name
        if executor is None:
            self.__init_executor()
        else:
            self.driver: WebDriver = executor

    def __init_executor(self):
        """初始浏览器驱动"""
        if self.driver_name == 'chrome':
            self.driver: WebDriver = webdriver.Chrome(ChromeDriverManager().install())
        elif self.driver_name == 'firefox':
            self.driver: WebDriver = webdriver.Firefox(GeckoDriverManager().install())
        elif self.driver_name == 'opera':
            self.driver: WebDriver = webdriver.Opera(OperaDriverManager().install())
        self.driver.implicitly_wait(5)

    def get_executor(self) -> WebDriver:
        """获取测试执行器"""
        return self.driver

    def quit_executor(self):
        if self.driver is not None:
            self.driver.quit()
            self.driver = None

    def open_url(self, url):
        """打开网址"""
        self.driver.get(url)

    # 定位元素
    def get_element(self, by, pattern=None) -> WebElement:
        """获取单个元素"""
        if by == 'id':
            return self.driver.find_element(By.ID, pattern)
        elif by == 'class':
            return self.driver.find_element(By.CLASS_NAME, pattern)
        elif by == 'tag_name':
            return self.driver.find_element(By.TAG_NAME, pattern)
        elif by == 'css':
            return self.driver.find_element(By.CSS_SELECTOR, pattern)
        elif by == 'xpath':
            return self.driver.find_element(By.XPATH, pattern)
        elif by == 'link_text':
            return self.driver.find_element(By.LINK_TEXT, pattern)
        elif by == 'partial_link_text':
            return self.driver.find_element(By.PARTIAL_LINK_TEXT, pattern)

    def get_elements(self, by, pattern) -> list[WebElement]:
        """获取一组元素"""
        if by == 'id':
            return self.driver.find_elements(By.ID, pattern)
        elif by == 'class':
            return self.driver.find_elements(By.CLASS_NAME, pattern)
        elif by == 'tag':
            return self.driver.find_elements(By.TAG_NAME, pattern)
        elif by == 'css':
            return self.driver.find_elements(By.CSS_SELECTOR, pattern)
        elif by == 'xpath':
            return self.driver.find_elements(By.XPATH, pattern)
        elif by == 'link_text':
            return self.driver.find_elements(By.LINK_TEXT, pattern)
        elif by == 'partial_link_text':
            return self.driver.find_elements(By.PARTIAL_LINK_TEXT, pattern)

    # 操作元素
    def click_element(self, by, pattern):
        """点击元素"""
        self.get_element(by=by, pattern=pattern).click()

    def clear_element(self, by, pattern):
        """清空元素"""
        self.get_element(by=by, pattern=pattern).clear()

    def send_keys(self, by, pattern, value):
        """输入信息"""
        self.get_element(by=by, pattern=pattern).send_keys(value)

    def submit(self, by, pattern):
        """提交表单"""
        self.get_element(by=by, pattern=pattern).submit()

    # 获取元素内容
    def get_text(self, by, pattern):
        """获取HTML元素的文本值"""
        return self.get_element(by=by, pattern=pattern).text

    def get_tag_name(self, by, pattern):
        """获取元素的标签类型"""
        return self.get_element(by=by, pattern=pattern).text

    def element_is_selected(self, by, pattern):
        """获取元素的选中状态"""
        return self.get_element(by=by, pattern=pattern).is_selected()

    def element_is_enabled(self, by, pattern):
        """获取元素的可编辑状态"""
        return self.get_element(by=by, pattern=pattern).is_enabled()

    def element_is_displayed(self, by, pattern):
        """判断元素是否已显示"""
        return self.get_element(by=by, pattern=pattern).is_displayed()

    # 获取元素的HTML属性
    def get_attribute(self, by, pattern, value):
        """获取元素的HTML属性"""
        return self.get_element(by=by, pattern=pattern).get_attribute(value)

    # 获取元素的DOM属性
    def get_property(self, by, pattern, value):
        """获取元素的DOM属性"""
        return self.get_element(by=by, pattern=pattern).get_property(value)

    # 弹出框操作
    def accept_alert(self):
        self.driver.switch_to.alert.accept()

    def dismiss_alert(self):
        self.driver.switch_to.alert.dismiss()

    def get_alert_text(self):
        return self.driver.switch_to.alert.text

    def send_keys_to_alert(self, value):
        self.driver.switch_to.alert.send_keys(value)

    # 多网页切换操作
    # 多浏览器窗口的切换
    def get_current_window_handle(self):
        """获得WebDriver当前正在操作的浏览器窗口句柄"""
        return self.driver.current_window_handle

    def get_window_handles(self):
        """获得该WebDriver实例下的全部句柄"""
        return self.driver.window_handles

    def switch_to_window(self, handle):
        """切换到指定窗口"""
        self.driver.switch_to.window(handle)

    # IFrame切换
    def switch_to_frame(self, frame):
        """切换到当前页面（或IFrame）的下一级指定IFrame中"""
        self.driver.switch_to.frame(frame)

    def switch_to_parent_frame(self):
        """切换到当前IFrame的上一级页面（或IFrame）中"""
        self.driver.switch_to.parent_frame()

    # 下拉框元素的选项操作
    def select_by_text(self, by, pattern, value):
        """按文本选择下拉框"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.select_by_visible_text(value)

    def select_by_index(self, by, pattern, value):
        """按索引选择下拉框"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.select_by_index(value)

    def select_by_value(self, by, pattern, value):
        """按选项值选择下拉框"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.select_by_value(value)

    def deselect_all(self, by, pattern):
        """取消所有选项"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.deselect_all()

    def deselect_by_value(self, by, pattern, value):
        """按选项值取消选项"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.deselect_by_value(value)

    def deselect_by_index(self, by, pattern, value):
        """按索引值取消选项"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.deselect_by_index(value)

    def deselect_by_visible_text(self, by, pattern, value):
        """按文本取消选项"""
        selectWebElement = Select(self.get_element(by, pattern))
        selectWebElement.deselect_by_visible_text(value)

    # driver.back()  # 后退
    # driver.forward()  # 前进
    # driver.refresh()  # 刷新
    # driver.minimize_window()  # 最小化窗口
    # driver.maximize_window()  # 最大化窗口
    # driver.close()  # 关闭窗口
    # driver.title  # 获取浏览器窗口当前的标题
    # driver.current_url  # 获取浏览器窗口当前的网址
    # driver.get_window_position()  # 获取位置对象
    # driver.get_window_size()  # 获取大小对象
    # driver.get_window_rect()  # 获取位置及大小对象
    # webElement.value_of_css_property("CSS属性名称")  # 获取元素的CSS属性
    # webElement.location  # 获取位置对象
    # webElement.size  # 获取大小对象
    # webElement.rect  # 获取位置及大小对象
    # SelectWebElement.first_selected_option  # 获取首个已选中项（类型为WebElement）
    # SelectWebElement.all_selected_options  # 获取全部已选中项（类型为WebElement数组）
    # SelectWebElement.options  # 获取下拉框提供的所有选项（无论是否已选中，类型为WebElement数组）


if __name__ == '__main__':
    driver = BaseWebDriver()
    url = 'http://www.ctesting.site:8080/login'
    driver.open_url(url)
    sleep(3)
    driver.send_keys('xpath', '//*[@id="pane-first"]/form/div[1]/div/div/input', 'caicaizi')
    sleep(2)
    driver.send_keys('xpath', '//*[@id="pane-first"]/form/div[2]/div/div/input', 'sfafsafasf')
    sleep(2)
    driver.click_element('xpath', '//*[@id="pane-first"]/form/div[4]/div/button')
    sleep(3)
    driver.quit_executor()

